<template>
  <div class="app-container">
    <!-- 头部区域 -->
    <el-form :inline="true" size="mini">
      <el-form-item label="用户名:">
        <el-input v-model.trim="query.username"></el-input>
      </el-form-item>
      <el-form-item label="手机号:">
        <el-input v-model.trim="query.mobile"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="queryData">
          查询
        </el-button>
        <el-button icon="el-icon-refresh" class="filter-item" @click="reload">
          重置
        </el-button>
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-circle-plus-outline"
          @click="handleAdd"
        >
          新增
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table :data="list" border highlight-current-row style="width: 100%">
      <!-- type="index"获取索引值，从1开始 ，label显示标题，prop 数据字段名，width列宽 -->
      <el-table-column align="center" type="index" label="序号" width="60">
      </el-table-column>
      <el-table-column align="center" prop="username" label="用户名">
      </el-table-column>
      <el-table-column align="center" prop="nickName" label="昵称">
      </el-table-column>
      <el-table-column align="center" prop="mobile" label="手机号">
      </el-table-column>
      <el-table-column align="center" prop="email" label="邮箱">
      </el-table-column>
      <el-table-column
        align="center"
        prop="isAccountNonExpired"
        label="帐号过期"
        width="80"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isAccountNonExpired === 0" type="danger">
            过期
          </el-tag>
          <el-tag v-if="scope.row.isAccountNonExpired === 1" type="success">
            正常
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="isEnabled"
        label="是否可用"
        width="80"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isEnabled === 0" type="danger">
            已删除
          </el-tag>
          <el-tag v-if="scope.row.isEnabled === 1" type="success">可用 </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="340">
        <!-- isEnabled == 1 才显示操作按钮-->
        <template slot-scope="scope" v-if="scope.row.isEnabled == 1">
          <el-button @click="handleEdit(scope.row.id)" size="mini"
            >编辑
          </el-button>
          <el-button
            @click="handleDelete(scope.row.id)"
            size="mini"
            type="danger"
          >
            删除
          </el-button>
          <el-button
            @click="handleRole(scope.row.id)"
            size="mini"
            type="success"
          >
            设置角色</el-button
          >
          <el-button @click="handlePwd(scope.row.id)" size="mini" type="success"
            >密码修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pageination
      :currentPage="page.current"
      :pageSizeInfo="page.size"
      :total="page.total"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <!-- 新增弹框 -->
    <edit
      :dialogVisible="edit.dialogVisible"
      :title="edit.title"
      :formData="edit.formData"
      :remoteForm="remoteForm"
    />
    <!-- 设置角色弹框 -->
    <el-dialog title="设置角色" :visible.sync="role.visible" width="65%">
      <!-- roleIds是当前用户所拥有的角色id, saveUserRole事件是子组件进行触发提交选择的角色id -->
      <Role :roleIds="role.roleIds" @saveUserRole="saveUserRole" />
    </el-dialog>
    <!-- 密码修改 -->
    <Password
      title="修改密码"
      :userId="pwd.userId"
      :visible="pwd.visible"
      :remoteClose="remotePwdClose"
    />
  </div>
</template>

<script>
import { ApiGetRoleList, ApiGetUserId, ApiDeleteUserId, ApiGetUserMenuIds, ApiPostUserRoleIds } from '@/api/user'
import pageination from '@/components/common/pageination.vue'
import edit from './components/edit.vue'
import Password from './components/password.vue'
import Role from '../role'
export default {
  name: 'User',
  components: {
    pageination,
    edit,
    Role,
    Password
  },
  data () {
    return {
      page: {
        current: 1,
        size: 20,
        total: 0
      },
      list: [],
      query: {},
      edit: {
        dialogVisible: false,
        title: '新增',
        formData: {}
      },
      role: { // 弹出设置角色组件
        visible: false,
        // 传递到子组件中时,至少会传递一个空数据[], 子组件判断是否有roleIds值 
        roleIds: [], // 封装当前用户所拥有的 角色id
        userId: null // 点击哪个用户，就是哪个用户id,当保存用户角色时，需要使用
      },
      pwd: { // 修改密码组件
        userId: null, //修改哪一个用户
        visible: false
      }
    }
  },
  methods: {
    async fetchData () {
      const { data: res } = await ApiGetRoleList(this.query, this.page.size, this.page.current)
      this.list = res.records
      this.page.total = res.total
    },
    // 当前条数
    handleSizeChange (val) {
      this.page.size = val
      this.fetchData()
    },
    // 当前页数
    handleCurrentChange (val) {
      this.page.current = val
      this.fetchData()
    },
    // 查 询
    queryData () {
      this.page.current = 1
      this.fetchData()
    },
    // 重 置
    reload () {
      this.query = {}
      this.fetchData()
    },
    // 新增
    handleAdd () {
      this.edit.title = '新增'
      this.edit.dialogVisible = true
    },
    async handleEdit (id) { // 编辑
      const res = await ApiGetUserId(id)
      if (res.code === 20000) {
        this.edit.formData = res.data
        this.edit.title = '编辑'
        this.edit.dialogVisible = true
      }
    },
    handleDelete (id) { // 删除
      this.$confirm('确认删除这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await ApiDeleteUserId(id)
        this.$message({
          type: res.code === 20000 ? 'success' : 'warn',
          message: res.message
        });
        // 刷新列表
        this.fetchData()
      }).catch(() => {

      });
    },
    handleRole (id) { // 设置角色
      this.role.userId = id
      // 查询用户拥有的ids
      ApiGetUserMenuIds(id).then(res => {
        // 赋值给ids
        this.role.roleIds = res.data
        // 弹出
        this.role.visible = true
      })
    },

    // 角色列表子组件会触发此方法来保存当前用户选择的角色id
    async saveUserRole (roleIds) {
      const res = await ApiPostUserRoleIds(this.role.userId, roleIds)
      if (res.code === 20000) {
        this.$message({
          message: '保存成功', type: 'success'
        })
        this.role.visible = false
      } else {
        this.$message({
          message: '保存失败', type: 'error'
        })
      }
    },
    handlePwd (id) { // 密码修改
      this.pwd.userId = id
      this.pwd.visible = true
    },
    remotePwdClose () { // 关闭密码修改弹窗
      this.pwd.userId = null
      this.pwd.visible = false
      this.fetchData()
    },
    remoteForm () { // 关闭弹窗
      this.edit.formData = {}
      this.edit.dialogVisible = false
      this.fetchData()
    },
  },
  created () {
    this.fetchData()
  },
}
</script>

<style>
</style>